package org.example.springbootexperiment03.service;

import org.example.springbootexperiment03.dao.RegionDao;
import org.example.springbootexperiment03.pojo.Region;
import org.example.springbootexperiment03.vo.ChartVO;
import org.example.springbootexperiment03.vo.DateNumberVO;
import org.example.springbootexperiment03.vo.LatestRegionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * Created by bothin
 * At 2020/5/7 14:19
 * Description:
 */
@Service
public class RegionServiceImpl implements IRegionService {
    @Autowired
    private RegionDao regionDao;

    @Override
    public List<Region> getALLRegions() {
        return this.regionDao.getALLRegions();
    }

    @Override
    public List<LatestRegionVO> getByCountryName(String countryName) {
        List<Region> result = new ArrayList<>();

        this.regionDao.getALLRegions().forEach((e) -> {
            if (e.getCountryName().toLowerCase().contains(countryName.toLowerCase())) {
                result.add(e);
            }
        });

        return converter(result);
    }


    @Override
    public Map<String,Object> getById(Integer id) {

        for (Region region : this.regionDao.getALLRegions()) {
            if (region.getId().equals(id)){
                List<DateNumberVO> rows = new ArrayList<>();
                for (String s : region.getData().keySet()) {
                    rows.add(new DateNumberVO(s,region.getData().get(s)));
                }
                Collections.reverse(rows);
                ChartVO chartVO = new ChartVO(Arrays.asList("date","number"),rows);

                //封装返回数据
                Map<String,Object> result = new HashMap<>();
                result.put("chartData",chartVO);
                result.put("region",region);
                return result;
            }

        }

        return null;
    }

    @Override
    public Map<String, Object> getLatestRegions() {
        Map<String,Object> result = new HashMap<>();
        Map<String, Long> dataMap = new LinkedHashMap<>();    //全球每天总数

        List<Region> allRegions = this.regionDao.getALLRegions();
        //初始化每天为0
        for (String s : allRegions.get(0).getData().keySet()) {
            dataMap.put(s, 0L);
        }
        allRegions.forEach((e) -> {
            for (String s : e.getData().keySet()) {
                //全球每天总数求和
                dataMap.put(s, dataMap.get(s) + e.getData().get(s));
            }
        });

        List<LatestRegionVO> list = converter(allRegions);
        String nowDate = (String) allRegions.get(0).getData().keySet().toArray()[0];
        Long globalNow = dataMap.get(nowDate);
        Long addedNow = 0L;
        for (LatestRegionVO latestRegionVO : list) {
            addedNow += latestRegionVO.getAddNumber();
        }
        List<DateNumberVO> rows = new ArrayList<>();
        for (String s : dataMap.keySet()) {
            rows.add(new DateNumberVO(s,dataMap.get(s)));
        }
        Collections.reverse(rows);
        ChartVO chartVO = new ChartVO(Arrays.asList("date","number"),rows);

        //封装返回数据
        result.put("globalNow", globalNow);  //目前病例总数
        result.put("addedNow", addedNow);    //目前新增病例
        result.put("nowDate", nowDate);      //目前统计时间
        result.put("globalData", list);      //各个地区情况
        result.put("dataList", chartVO);     //全球每天总数
        return result;
    }

    /**
     * 对象转换 List<Region> -> List<LatestRegionVO>
     * @param list List<Region>
     * @return List<LatestRegionVO>
     */
    public List<LatestRegionVO> converter(List<Region> list){
        List<LatestRegionVO> regionVOList = new ArrayList<>();
        list.forEach((e) -> {
            Long now = 0L;
            Long end = 0L;
            int count = 0;  //取前两个求新增数量
            for (String s : e.getData().keySet()) {
                if (count == 0) now = e.getData().get(s);
                else if (count == 1) end = e.getData().get(s);
                else
                    break;
                count++;
            }
            LatestRegionVO latestRegionVO = new LatestRegionVO(e.getId(), e.getProvinceName(), e.getCountryName(), now, now - end);
            regionVOList.add(latestRegionVO);
        });
        //按照目前病例数排序（倒序）
        List<LatestRegionVO> sortedList =  regionVOList.stream().sorted(Comparator.comparing(LatestRegionVO::getNow).reversed()).collect(Collectors.toList());
        return sortedList;
    }
}
